package com.iris.live.services.report.sources;

import com.google.common.base.Strings;
import com.iris.live.services.data.repositories.InsuranceRepository;
import com.iris.live.services.models.PageData;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.IExcelReportSource;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Date;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by lusong on 2017/5/10.
 */
@Component(value = "ExportInsuranceAccountInfoSource")
public class ExportInsuranceAccountInfoSource implements IExcelReportSource {

    private Logger logger = LoggerFactory.getLogger(ExportInsuranceAccountInfoSource.class);

    @Autowired
    private InsuranceRepository insuranceRepository;

    @Override
    public Sheet[] getSheets(Map<String, Object> context) {
        Sheet[] sheets = new Sheet[1];
        sheets[0] = new Sheet("经销商_保险业务台账导出模板","经销商_保险业务台账导出模板",true);
         return sheets;
    }

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) throws ParseException {
        String permit = context.get("permit") == null ? null : context.get("permit").toString();
        String tciNo = context.get("tciNo") == null ? null : context.get("tciNo").toString();
        String vciNo = context.get("vciNo") == null ? null : context.get("vciNo").toString();
        String tciCompany = context.get("tciCompany") == null ? null : context.get("tciCompany").toString();
        String vciCompany = context.get("vciCompany") == null ? null : context.get("vciCompany").toString();
        String salesName = context.get("salesName") == null ? null : context.get("salesName").toString();
        String status = context.get("status") == null ? null : context.get("status").toString();
        String username = context.get("username") == null ? null : context.get("username").toString();
        String userPhone = context.get("userPhone") == null ? null : context.get("userPhone").toString();
        String vin = context.get("vin") == null ? null : context.get("vin").toString();
        String type = context.get("type") == null ? null : context.get("type").toString();
        String startEnterDateStr = context.get("startEnterDate") == null ? null : context.get("startEnterDate").toString();
        String endEnterDateStr = context.get("endEnterDate") == null ? null : context.get("endEnterDate").toString();
        String startDueDateStr = context.get("startDueDate") == null ? null : context.get("startDueDate").toString();
        String endDueDateStr = context.get("endDueDate") == null ? null : context.get("endDueDate").toString();
        String startOrderDateStr = context.get("startOrderDate") == null ? null : context.get("startOrderDate").toString();
        String endOrderDateStr = context.get("endOrderDate") == null ? null : context.get("endOrderDate").toString();
        String startExpectInvoiceDateStr = context.get("startExpectInvoiceDate") == null ? null : context.get("startExpectInvoiceDate").toString();
        String endExpectInvoiceDateStr = context.get("endExpectInvoiceDate") == null ? null : context.get("endExpectInvoiceDate").toString();
        String orderRelyType = context.get("orderRelyType") == null ? null : context.get("orderRelyType").toString();
        String pageSizeStr = context.get("pageSize") == null ? null : context.get("pageSize").toString();
        String pageIndexStr = context.get("pageIndex") == null ? null : context.get("pageIndex").toString();
        String forceInsuranceStartDate = context.get("forceInsuranceStartDate") == null ? null : context.get("forceInsuranceStartDate").toString();
        String forceInsuranceEndDate = context.get("forceInsuranceEndDate") == null ? null : context.get("forceInsuranceEndDate").toString();
        String commercialInsuranceStartDate = context.get("commercialInsuranceStartDate") == null ? null : context.get("commercialInsuranceStartDate").toString();
        String commercialInsuranceEndDate = context.get("commercialInsuranceEndDate") == null ? null : context.get("commercialInsuranceEndDate").toString();

        int pageSize = 0, pageIndex = 0;
        Date startEnterDate = null, endEnterDate= null, startDueDate = null, endDueDate = null, startOrderDate = null,
                endOrderDate = null, startExpectInvoiceDate = null, endExpectInvoiceDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (!Strings.isNullOrEmpty(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }
        if (!Strings.isNullOrEmpty(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        if (!Strings.isNullOrEmpty(startEnterDateStr)) {
            startEnterDate = new Date(sdf.parse(startEnterDateStr).getTime());
        }
        if (!Strings.isNullOrEmpty(endEnterDateStr)) {
            endEnterDate = new Date(sdf.parse(endEnterDateStr).getTime());
        }
        if (!Strings.isNullOrEmpty(startDueDateStr)) {
            startDueDate = new Date(sdf.parse(startDueDateStr).getTime());
        }
        if (!Strings.isNullOrEmpty(endDueDateStr)) {
            endDueDate = new Date(sdf.parse(endDueDateStr).getTime());
        }
        if (!Strings.isNullOrEmpty(startOrderDateStr)) {
            startOrderDate = new Date(sdf.parse(startOrderDateStr).getTime());
        }
        if (!Strings.isNullOrEmpty(endOrderDateStr)) {
            endOrderDate = new Date(sdf.parse(endOrderDateStr).getTime());
        }
        if (!Strings.isNullOrEmpty(startExpectInvoiceDateStr)) {
            startExpectInvoiceDate = new Date(sdf.parse(startExpectInvoiceDateStr).getTime());
        }
        if (!Strings.isNullOrEmpty(endExpectInvoiceDateStr)) {
            endExpectInvoiceDate = new Date(sdf.parse(endExpectInvoiceDateStr).getTime());
        }

        List<Row> list = new ArrayList<>();

        list.add(new Row());
        list.add(new Row());
        list.add(new Row());
        list.add(new Row());
        list.add(new Row());
        list.add(new Row());

        PageData pageData = insuranceRepository.getInsuranceAccountInfoOverWrite("", pageSize, pageIndex, permit, tciNo, vciNo,
                tciCompany, vciCompany, salesName,status, username, userPhone, vin, type, startEnterDate, endEnterDate,
                startDueDate, endDueDate, startOrderDate, endOrderDate, startExpectInvoiceDate, endExpectInvoiceDate, orderRelyType,
                forceInsuranceStartDate,forceInsuranceEndDate,commercialInsuranceStartDate,commercialInsuranceEndDate);
        if (pageData != null) {
            List vaules = pageData.getData();
            if (vaules != null && !vaules.isEmpty()) {
                for (Object o : vaules) {
                    Map map = (Map) o;
                    list.add(new Row(getCells(map)));
                }
            }
        }

        return list;
    }

    private Cell[] getCells(Map<String, Object> map) {
        Cell[] cells = new Cell[64];
        int index = 0;
        DecimalFormat df = new DecimalFormat("##0.00");
        cells[index++] = new Cell(map.get("所属保险专员") == null ? "" : map.get("所属保险专员").toString(), index);
        cells[index++] = new Cell(map.get("第N年") == null ? "" : map.get("第N年").toString(), index);
        cells[index++] = new Cell(map.get("投保类型") == null ? "" : map.get("投保类型").toString(), index);
        cells[index++] = new Cell(map.get("保险销售状态") == null ? "" : map.get("保险销售状态").toString(), index);
        cells[index++] = new Cell(map.get("客户姓名") == null ? "" : map.get("客户姓名").toString(), index);
        cells[index++] = new Cell(map.get("客户电话") == null ? "" : map.get("客户电话").toString(), index);
        cells[index++] = new Cell(map.get("销售顾问") == null ? "" : map.get("销售顾问").toString(), index);
        cells[index++] = new Cell(map.get("意向车系") == null ? "" : map.get("意向车系").toString(), index);
        cells[index++] = new Cell(map.get("意向车型") == null ? "" : map.get("意向车型").toString(), index);
        cells[index++] = new Cell(map.get("VIN码") == null ? "" : map.get("VIN码").toString(), index);
        cells[index++] = new Cell(map.get("登记日期") == null ? "" : map.get("登记日期").toString(), index);
        cells[index++] = new Cell(map.get("交强险保单号") == null ? "" : map.get("交强险保单号").toString(), index);
        cells[index++] = new Cell(map.get("交强险保险公司") == null ? "" : map.get("交强险保险公司").toString(), index);
        cells[index++] = new Cell(map.get("交强险") == null ? "" : map.get("交强险").toString(), index);
        cells[index++] = new Cell(map.get("交强险出单日期") == null ? "" : map.get("交强险出单日期").toString(), index);
        cells[index++] = new Cell(map.get("交强险手续费") == null ? "" : map.get("交强险手续费").toString(), index);
        cells[index++] = new Cell(map.get("交强险开始日期") == null ? "" : map.get("交强险开始日期").toString(), index);
        cells[index++] = new Cell(map.get("交强险结束日期") == null ? "" : map.get("交强险结束日期").toString(), index);
        cells[index++] = new Cell(map.get("车船税") == null ? "" : map.get("车船税").toString(), index);
        cells[index++] = new Cell(map.get("商业险保单号") == null ? "" : map.get("商业险保单号").toString(), index);
        cells[index++] = new Cell(map.get("商业险保险公司") == null ? "" : map.get("商业险保险公司").toString(), index);
        cells[index++] = new Cell(map.get("一保多年商业险保费") == null ? "" : df.format(Double.parseDouble(map.get("一保多年商业险保费").toString())), index);
        cells[index++] = new Cell(map.get("交强险出单日期") == null ? "" : map.get("交强险出单日期").toString(), index);
        cells[index++] = new Cell(map.get("商业险手续费") == null ? "" : map.get("商业险手续费").toString(), index);
        cells[index++] = new Cell(map.get("商业险开始日期") == null ? "" : map.get("商业险开始日期").toString(), index);
        cells[index++] = new Cell(map.get("商业险结束日期") == null ? "" : map.get("商业险结束日期").toString(), index);
        cells[index++] = new Cell(map.get("批单类型") == null ? "" : map.get("批单类型").toString(), index);
        cells[index++] = new Cell(map.get("车辆损失险/保额") == null ? "" : map.get("车辆损失险/保额").toString(), index);
        cells[index++] = new Cell(map.get("车辆损失险/保费") == null ? "" : map.get("车辆损失险/保费").toString(), index);
        cells[index++] = new Cell(map.get("车辆损失险/不计免赔") == null ? "" : map.get("车辆损失险/不计免赔").toString(), index);
        cells[index++] = new Cell(map.get("商业三者责任险/保额") == null ? "" : map.get("商业三者责任险/保额").toString(), index);
        cells[index++] = new Cell(map.get("商业三者责任险/保费") == null ? "" : map.get("商业三者责任险/保费").toString(), index);
        cells[index++] = new Cell(map.get("商业三者责任险/不计免赔") == null ? "" : map.get("商业三者责任险/不计免赔").toString(), index);
        cells[index++] = new Cell(map.get("全车盗抢险/保费") == null ? "" : map.get("全车盗抢险/保费").toString(), index);
        cells[index++] = new Cell(map.get("全车盗抢险/不计免赔") == null ? "" : map.get("全车盗抢险/不计免赔").toString(), index);
        cells[index++] = new Cell(map.get("车上人员责任险-司机/保额") == null ? "" : map.get("车上人员责任险-司机/保额").toString(), index);
        cells[index++] = new Cell(map.get("车上人员责任险-司机/保费") == null ? "" : map.get("车上人员责任险-司机/保费").toString(), index);
        cells[index++] = new Cell(map.get("车上人员责任险-司机/不计免赔") == null ? "" : map.get("车上人员责任险-司机/不计免赔").toString(), index);
        cells[index++] = new Cell(map.get("车上人员责任险-乘客/保额") == null ? "" : map.get("车上人员责任险-乘客/保额").toString(), index);
        cells[index++] = new Cell(map.get("车上人员责任险-乘客/保费") == null ? "" : map.get("车上人员责任险-乘客/保费").toString(), index);
        cells[index++] = new Cell(map.get("车上人员责任险-乘客/不计免赔") == null ? "" : map.get("车上人员责任险-乘客/不计免赔").toString(), index);
        cells[index++] = new Cell(map.get("玻璃单独破碎险/保额") == null ? "" : map.get("玻璃单独破碎险/保额").toString(), index);
        cells[index++] = new Cell(map.get("玻璃单独破碎险/保费") == null ? "" : map.get("玻璃单独破碎险/保费").toString(), index);
        cells[index++] = new Cell(map.get("车身划痕损失险/保额") == null ? "" : map.get("车身划痕损失险/保额").toString(), index);
        cells[index++] = new Cell(map.get("车身划痕损失险/保费") == null ? "" : map.get("车身划痕损失险/保费").toString(), index);
        cells[index++] = new Cell(map.get("车身划痕损失险/不计免赔") == null ? "" : map.get("车身划痕损失险/不计免赔").toString(), index);
        cells[index++] = new Cell(map.get("自燃损失险/保费") == null ? "" : map.get("自燃损失险/保费").toString(), index);
        cells[index++] = new Cell(map.get("发动机涉水损失险/保费") == null ? "" : map.get("发动机涉水损失险/保费").toString(), index);
        cells[index++] = new Cell(map.get("倒车镜、车灯单独损失险/保费") == null ? "" : map.get("倒车镜、车灯单独损失险/保费").toString(), index);
        cells[index++] = new Cell(map.get("新增设备损失险/保额") == null ? "" : map.get("新增设备损失险/保额").toString(), index);
        cells[index++] = new Cell(map.get("新增设备损失险/保费") == null ? "" : map.get("新增设备损失险/保费").toString(), index);
        cells[index++] = new Cell(map.get("指定修理厂险/保费") == null ? "" : map.get("指定修理厂险/保费").toString(), index);
        cells[index++] = new Cell(map.get("修理期间费用补偿险/保额") == null ? "" : map.get("修理期间费用补偿险/保额").toString(), index);
        cells[index++] = new Cell(map.get("修理期间费用补偿险/保费") == null ? "" : map.get("修理期间费用补偿险/保费").toString(), index);
        cells[index++] = new Cell(map.get("车上货物责任险/保额") == null ? "" : map.get("车上货物责任险/保额").toString(), index);
        cells[index++] = new Cell(map.get("车上货物责任险/保费") == null ? "" : map.get("车上货物责任险/保费").toString(), index);
        cells[index++] = new Cell(map.get("精神损害抚慰金责任险/保额") == null ? "" : map.get("精神损害抚慰金责任险/保额").toString(), index);
        cells[index++] = new Cell(map.get("精神损害抚慰金责任险/保费") == null ? "" : map.get("精神损害抚慰金责任险/保费").toString(), index);
        cells[index++] = new Cell(map.get("机动车损失保险无法找到第三方特约险/保费") == null ? "" : map.get("机动车损失保险无法找到第三方特约险/保费").toString(), index);
        cells[index++] = new Cell(map.get("自定义险种一/保额") == null ? "" : map.get("自定义险种一/保额").toString(), index);
        cells[index++] = new Cell(map.get("自定义险种一/保费") == null ? "" : map.get("自定义险种一/保费").toString(), index);
        cells[index++] = new Cell(map.get("自定义险种二/保额") == null ? "" : map.get("自定义险种二/保额").toString(), index);
        cells[index++] = new Cell(map.get("自定义险种二/保费") == null ? "" : map.get("自定义险种二/保费").toString(), index);
        cells[index++] = new Cell(map.get("附加险/不计免赔") == null ? "" : map.get("附加险/不计免赔").toString(), index);
        return cells;
    }
}
